import { getRoleAll, getRoleList, getRole, getRoleTree, delRole, saveRole, updateRole } from '@/api/sys/role'

const actions = {
  // 获取所有角色列表
  getRoleAll({ commit }) {
    return new Promise((resolve, reject) => {
      getRoleAll().then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取角色列表
  getRoleList({ commit }, param) {
    return new Promise((resolve, reject) => {
      getRoleList(param).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getRole({ commit }, id) {
    return new Promise((resolve, reject) => {
      getRole(id).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  updateRole({ commit }, param) {
    return new Promise((resolve, reject) => {
      updateRole(param).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  /**
   * 保存菜单
   * @param {菜单} data
   */
  saveRole({ commit }, param) {
    return new Promise((resolve, reject) => {
      saveRole(param).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  /**
   *  删除菜单
   * @param {id} id
   */
  delRole({ commit }, id) {
    return new Promise((resolve, reject) => {
      delRole(id).then(res => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }

}

export default {
  namespaced: true,
  actions
}
